package com.googlecode.uipipedream.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Observable;

import com.googlecode.uipipedream.spec.ColourModel;

/**
 * @author <a href="http://daemonsadvocate.blogspot.com/">Matthew B. Gray</a>
 */
public class HorizontalSliderBar extends VerticalSliderBar {
	private static final long serialVersionUID = 4988289504165404166L;

	public HorizontalSliderBar(ColourModel model, int type) { super(model, type); }
	
	@Override public void update(Observable o, Object arg) { repaint(); }
	@Override public synchronized void paint(Graphics g) { update(g); }
	@Override public Dimension preferredSize() { return new Dimension(500, 80); }
	@Override public Dimension minimumSize() { return new Dimension(500, 80); }
	
	public synchronized void update(Graphics g) {
		// 0. Cache dimensions and setup object.
		Dimension d = getSize();
		if ((offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height)) {
			offDimension = d;
			offImage = createImage(d.width, d.height);
			offGraphics = offImage.getGraphics();
			// border of 4 all round, 8 at left
			box = new Rectangle(4, 4, d.width - 4, d.height - 4);
		}

		// 1. Erase the previous image.
		offGraphics.setColor(getBackground());
		offGraphics.fillRect(0, 0, d.width, d.height);
		offGraphics.setColor(Color.black);

		// 2. Paint the brightness bar
		for (int i = 0; i < box.width; ++i) {
			float offset = 1f - (float) i / box.width;
			switch (type) {
			case HUE:
				offGraphics.setColor(Color.getHSBColor(offset, model.getSaturation(), model.getBrightness()));
				break;
			case SATURATION:
				offGraphics.setColor(Color.getHSBColor(model.getHue(), offset, model.getBrightness()));
				break;
			case BRIGHTNESS:
				offGraphics.setColor(Color.getHSBColor(model.getHue(), model.getSaturation(), offset));
				break;
			}
			offGraphics.drawLine(box.x + i, box.y, box.x +i, box.y + box.height - 1);
		}
		offGraphics.setColor(Color.white);
		offGraphics.drawRect(box.x, box.y, box.width - 1, box.height - 1);

		// 3. Paint brightness cursor
		float offset = 0;
		switch (type) {
		case HUE:
			offset = model.getHue();
			break;
		case SATURATION:
			offset = model.getSaturation();
			break;
		case BRIGHTNESS:
			offset = model.getBrightness();
			break;
		}
		int xx = box.y + (int) (box.width * (1.0 - offset));
		offGraphics.drawLine(xx, box.y, xx, box.y + box.width - 1);
		offGraphics.drawLine(xx, box.y - 2, xx + 3, box.y - 7);
		offGraphics.drawLine(xx, box.y - 2, xx - 3, box.y - 7);
		offGraphics.drawLine(xx, box.y - 7, xx + 3, box.y - 7);

		g.drawImage(offImage, 0, 0, this);
	}

	public boolean mouseDrag(Event e, int x, int y) {
		if (draggingBox) {
			float offset = 1f - (float) (x - box.x) / box.width;
			if (offset < 0) offset = 0f;
			if (offset > 1f) offset = 1f;
			
			switch (type) {
			case HUE:
				model.setHue(offset);
				break;
			case SATURATION:
				model.setSaturation(offset);
				break;
			case BRIGHTNESS:
				model.setBrightness(offset);
				break;
			}
			return true;
		}
		return false;
	}

	public boolean mouseDown(Event e, int x, int y) {
		draggingBox = false;

		if (box.contains(x, y)) {
			draggingBox = true;
			float offset = 1f - (float) (x - box.x) / box.width;
			switch (type) {
			case HUE:
				model.setHue(offset);
				break;
			case SATURATION:
				model.setSaturation(offset);
				break;
			case BRIGHTNESS:
				model.setBrightness(offset);
				break;
			}
			return true;
		}
		
		float curr = 0;
		switch (type) {
		case HUE:
			curr = model.getHue();
			break;
		case SATURATION:
			curr = model.getSaturation();
			break;
		case BRIGHTNESS:
			curr = model.getBrightness();
			break;
		}
		
		int dx = x - box.x - (int) (box.width * (1f - curr));
		if (y < box.y && y >= box.y - 8 && dx >= -4 && dx <= 4) {
			draggingBox = true;
			return true;
		}
		return false;
	}
}
